﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WTwitter.Model.Twitter.Json;
using WTwitter.Model.Framework;
using WTwitter.Model.Framework.TextSplitter;
using WTwitter.Model.UserData;
using WTwitter.Model.Twitter.Utility;
using System.Globalization;

namespace WTwitter.Model.Twitter {
	/// <summary>
	/// Twitterから取得した１アイテムを表すクラス
	/// </summary>
	public class TwitterItem : ITimelineItem {
		#region private member
		private string _source;
		private string _text;
		private Status _item;
		private IUser _user;
		private string _detailDescription;
		private List<TextComponent> _textComponent;
		#endregion

		public TwitterItem(Status item) {
			_item = item;
			_user = new TwitterUser(_item.User);
		}

		#region ITimelineItem メンバ

		public ItemType Type {
			get { return ItemType.TwitterStatus; }
		}

		public string TypeOfSourceTimeline {
			get { return "Twitter"; }
		}

		public long Id {
			get { return _item.Id; }
		}

		public long? InReplyTo {
			get { return _item.InReplyToStatusId; }
		}

		public IUser User {
			get { return _user; }
		}

		public string Text {
			get {
				if (string.IsNullOrEmpty(_text)) {
					_text = HttpUtility.HtmlDecode(_item.Text);
				}
				return _text;
			}
		}

		private DateTime? _createdAt = null;
		public DateTime CreatedAt {
			get {
				//文字列で取得したものをDateTimeに変換する
				if (_createdAt == null) {
					_createdAt = DateTimeConverter.StringToDateTime(CreatedAtString);
				}
				return _createdAt.Value;
			}
		}

		public List<TextComponent> TextComponents {
			get {
				if (_textComponent == null) {
					_textComponent = Splitter.Split(Text);
				}
				return _textComponent; 
			}
		}

		public bool Favorited {
			private set { _item.Favorited = value; }
			get { return _item.Favorited; }
		}

		public long? InReplyToUserId {
			get { return _item.InReplyToUserId; }
		}

		public long? InReplyToStatusId {
			get { return _item.InReplyToStatusId; }
		}

		public string InReplyToScreenName {
			get { return _item.InReplyToScreenName; }
		}

		private string CreatedAtString {
			get { return _item.CreatedAtString; }
		}

		public string Source {
			get {
				if (string.IsNullOrEmpty(_source)) {
					_source = HttpUtility.UrlDecode(_item.Source);
				}
				return _source; 
			}
		}

		public bool Truncated {
			get { return _item.Truncated; }
		}

		public string DetailDescription {
			get {
				if (string.IsNullOrEmpty(_detailDescription)) {
					_detailDescription = string.Format(
						"Id: {1}{0}ScreenName: {2}{0}Name: {3}{0}CreatedAt: {4}{0}Source: {5}{0}"
							+ "InReplyToStatusId: {6}{0}InReplyToScreenName: {7}{0}InReplyToUserId: {8}{0}"
							+ "Favorited: {9}{0}Truncated: {10}{0}",
						Environment.NewLine,
						Id,
						User.ScreenName,
						User.Name,
						CreatedAtString,
						Source,
						InReplyToStatusId.ToString(),
						InReplyToScreenName,
						InReplyToUserId.ToString(),
						Favorited.ToString(),
						Truncated.ToString()
						);
				}
				return _detailDescription;
			}
		}

		#endregion

		#region Public Method
		/// <summary>
		/// お気に入りを付ける/消す
		/// </summary>
		/// <param name="favorited">付ける場合はtrue/消す場合はfalse</param>
		/// <param name="info">認証情報</param>
		/// <returns>変更後のFavoritedの値。</returns>
		public bool RequestToChangeFavoritedValue(bool favorited, AuthInfo info) {
			Status result;
			if (!Favorited) {
				result = FavoritesMethod.Create(this.Id, info);
			} else {
				result = FavoritesMethod.Destroy(this.Id, info);
			}
			_item.Favorited = !_item.Favorited;

			return _item.Favorited;
		}
		#endregion

		#region IEquatable<ITimelineItem> メンバ

		public bool Equals(ITimelineItem other) {
			return this.Id == other.Id;
		}

		#endregion

	}
}
